package com.hugoo.form.slt;

import com.hugoo.core.util.TL;
import com.hugoo.rpt.PageInfo;
import java.io.File;
import java.io.IOException;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Lovelock.Chan
 */
public abstract class ExpAction implements PageInfo {

    protected final Log log = LogFactory.getLog(this.getClass());
    private String sql, fileName, coli, dbl;
    protected String[][] col;
    protected File tmpFile;

    public ExpAction(String dbl, String sql, String fileName, String col) {
        this.dbl = dbl;
        this.sql = sql;
        this.fileName = fileName;
        this.coli = col;
        _init_col();
    }

    private void _init_col() {
        List<String> t1 = new ArrayList(), t2 = new ArrayList();
        String[] cs, cols = coli.split(",");
        for (String cl : cols) {
            if (TL.isEmpty(cl)) {
                continue;
            }
            cs = cl.split(":");
            if (cs.length != 2) {
                log.info("过滤无效列【" + cl + "】");
                continue;
            }
            t1.add(cs[0]);
            t2.add(cs[1]);
        }
        this.col = new String[][]{t1.toArray(new String[0]), t2.toArray(new String[0])};
    }

    public File getTempFile() throws IOException {
        tmpFile = File.createTempFile(UUID.randomUUID().toString(), null);
        return tmpFile;
    }

    public void validateColumn(ResultSetMetaData metaData) throws SQLException {
        //
    }

    @Override
    public String getDbl() {
        return dbl;
    }

    @Override
    public String getVersion() {//本部分,暂时先不设计版本一说
        return "";
    }

    public boolean isExpTitle() {
        return true;
    }

    public boolean haveEnd() {
        return true;
    }

    public String getTitle() {
        return "";
    }

    public String getFNUMColumns() {
        return "";
    }

    public File getFile() {
        return tmpFile;
    }

    @Override
    public String getQuerySql() {
        return sql;
    }

    public String getFileName() {
        return fileName;
    }
}
